import axios from "axios";

const sm3 = require("sm-crypto").sm3;

function getUUID() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";
  var uuid = s.join("");
  return uuid;
}

const baseUrl = "http://25.55.39.140:21100";

const sjccKeyProd = {
  appkey: "254fdbcf36d049b98d45226f4cfbe373",
  appsecret: "e25b50f1-417c-4dc3-986b-827dd71ec4cd"
};

const setHeadersForToken = () => {
  const timestamp = new Date().getTime();
  const random = getUUID();
  const ysStr = `timestamp=${timestamp}&random=${random}&secretkey=${sjccKeyProd.appsecret}`;
  const sign = sm3(ysStr).toUpperCase();
  // const token = sessionStorage.getItem("gisMapToken");
  const headers = { timestamp, random, sign };
  return headers;
};

const setHeadersForOthers = () => {
  const timestamp = new Date().getTime();
  const random = getUUID();
  const token = sessionStorage.getItem("gisMapToken");
  const ysStr = `token=${token}&timestamp=${timestamp}&random=${random}&secretkey=${sjccKeyProd.appsecret}`;
  const sign = sm3(ysStr).toUpperCase();
  const headers = { timestamp, random, sign, token };
  return headers;
};
export const getGisToken = () => {
  const headers = setHeadersForToken();
  return axios.post(`${baseUrl}/authorityservice/login/integrate`, { appkey: sjccKeyProd.appkey }, { headers });
};
// 图类型字典查询
export const getGisTlxZd = () => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/maptype/def`, { headers });
};

// 根据图类型查询图层集合
export const getGisTclxjh = (mtno) => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/maptype/${mtno}/layers/def`, { headers });
};
// 要素类型分组字典查询
export const getGisYslxFzzd = () => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/psrtypegroup/def`, { headers });
};

// 获取所有网省中心点信息

export const provincedetails = () => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/organization/provincedetails`, { headers });
};

// 根据网省 ID 获取地市中心点
export const ssdsdetails = () => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/organization/D4FA8CE2B4414C6FAD8B481A2595FF66/ssdsdetails`, { headers });
};
export const yxdwdetails = () => {
  const headers = setHeadersForOthers();
  return axios.get(`${baseUrl}/metadataservice/organization/E9BA9EE1E2814073BF8680780695261D/yxdwdetails`, { headers });
};
// 通用要素
export const getTongyong = (mtno) => {
  const headers = setHeadersForOthers();
  const params = [
    {
      id: mtno.typeId,
      iscodetsf: "true",
      featureList: [
        {
          // "layerDefs": "dydj=35"
          // "layerDefs": "sbzlx=30000000",
          // "layerDefs": "sbmc='木兰变电站' and sbzlx=30000000"
          layerDefs: mtno.layerDefs
          //  layerFields: "sbmc,sbid,dydj,sbzlx,yxdw,ssds"
          // layerDefs: "yxdw='D4FA8CE2B4414C6FAD8B481A2595FF66'"
        }
      ]
    }
  ];
  return axios.post(`${baseUrl}/queryservice/psrquery/queryequipfeatures`, params, { headers });
};
